package com.szosen.jkzj.activity.ggws;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.szosen.database.DBService;
import com.szosen.database.OldPeopleSelfCare;
import com.szosen.jkzj.R;
import com.szosen.jkzj.activity.BaseActivity;
import com.szosen.util.TwoCloumAdapter;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LnrshzlnlList extends BaseActivity {
    private SQLiteDatabase db;
    private ListView lvCheckTableList;
    TextView txt_title;
    Button btn_new;
    private TwoCloumAdapter adapter;
    private static final int HEALTH_CHECK = 2;
    private List<String[]> datas;

    private boolean checkTextisNull(String text) {
        if (text == null) {
            return true;
        }
        if (TextUtils.isEmpty(text) || text.equals("null")) {
            return true;
        }
        return false;
    }

    public void finishActivity(View view) {
        onBackPressed();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lnrshzlnl_list);
        super.setTitleContent("【老年人生活自理能力】");
        lvCheckTableList = (ListView) findViewById(R.id.lv_check_table_list);
        btn_new = (Button) findViewById(R.id.btn_htal_new);
        btn_new.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                addNewCheckTable();
            }

        });
        lvCheckTableList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // Map<String, String> m = (Map) parent
                // .getItemAtPosition(position);
                // 0为责任医生，1为体检时间
                String[] ss = (String[]) parent.getItemAtPosition(position);
                Intent intent = new Intent(LnrshzlnlList.this, Lnrshzlnl.class);
                intent.putExtra("time", ss[0]);// 用体检日期为唯一标准查询或者插入
                intent.putExtra("flag", "old");// 用体检日期为唯一标准查询或者插入
                startActivity(intent);
            }
        });

        lvCheckTableList
                .setOnItemLongClickListener(new OnItemLongClickListener() {

                    @Override
                    public boolean onItemLongClick(final AdapterView<?> parent,
                                                   View view, final int position, long id) {

                        AlertDialog.Builder builder = new AlertDialog.Builder(
                                LnrshzlnlList.this);
                        builder.setMessage("您确定要删除此记录？");
                        builder.setPositiveButton("确定", new OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                String[] ss = (String[]) parent
                                        .getItemAtPosition(position);
                                // 用体检日期为唯一标准查询或者插入
                                String time = ss[0];
                                OldPeopleSelfCare o = new OldPeopleSelfCare();
                                o.setTime(time);
                                DBService.delete(o);
                                loadData();
                            }
                        });
                        builder.setNegativeButton("取消", null);
                        builder.create().show();
                        return true;
                    }
                });

    }

    @Override
    @SuppressLint("ResourceAsColor")
    protected void onResume() {
        super.onResume();
        loadData();
    }

    protected void updateDatabase(int pCoverReuslt, JSONObject pJson) {
    }

    private void loadData() {
        OldPeopleSelfCare ops = new OldPeopleSelfCare();
        ops.setIdCard(BaseActivity.getIdCard());
        List<OldPeopleSelfCare> lists = (List<OldPeopleSelfCare>) DBService
                .queryByIdCard(ops);
//        System.out.println("老年人生活自理能力的列表长度" + lists.size());
        datas = new ArrayList<String[]>();
        for (int j = 0; j < lists.size(); j++) {
            OldPeopleSelfCare op = lists.get(j);
            String[] data = new String[]{op.getCheckid(), op.getInputDate()};
            datas.add(data);
        }
        // adapter = new SimpleAdapter(HealthCheckList.this, datas,
        // R.layout.check_table_list_item, new String[] {
        // "doctorName", "checkDate" }, new int[] { R.id.tv_data1,
        // R.id.tv_data2, });
        adapter = new TwoCloumAdapter(context, datas);
        lvCheckTableList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    protected void addNewCheckTable() {
        Intent intent = new Intent();
        intent.setClass(LnrshzlnlList.this, Lnrshzlnl.class);
        intent.putExtra("flag", "new");
        startActivity(intent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        adapter.notifyDataSetChanged();
    }

}
